package com.lzk.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzk.common.R;
import com.lzk.pojo.Employee;
import com.lzk.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        String password = employee.getPassword();
        String username = employee.getUsername();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.eq(Employee::getUsername,username);
        Employee emp = employeeService.getOne(employeeLambdaQueryWrapper);
        if (emp == null){
            return R.error("用户名不存在");
        }
        String empPassword = emp.getPassword();
        if (!empPassword.equals(md5Password)){
            return R.error("密码错误");
        }
        if (emp.getStatus() == 0){
            return R.error("员工状态已禁用");
        }
        Long empId = emp.getId();
        HttpSession session = request.getSession();
        session.setAttribute("emp",empId);
        return R.success(emp);
    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("emp");
        return R.success("退出成功");
    }
    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee){
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
//        Long emp = (Long) session.getAttribute("emp");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateUser(emp);
//        employee.setCreateUser(emp);
        employeeService.save(employee);
        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page<Employee>> page(int page,int pageSize,String name){
        Page<Employee> infoPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null,Employee::getName,name);
        Page<Employee> employeePage = employeeService.page(infoPage, lqw);
        return R.success(employeePage);
    }
    @PutMapping
    public R<String>update(@RequestBody Employee employee){
//        Long empId= (Long) session.getAttribute("emp");
//        employee.setUpdateUser(empId);
//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    //回显数据
    @GetMapping("/{id}")
    public R<Employee> getEmployeeById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

}
